package homework9.Convert;

import homework9.Bean.School;
import org.apache.struts2.util.StrutsTypeConverter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SchoolConverter extends StrutsTypeConverter {
    List<School> schoolList = new ArrayList<>();

    @Override
    public Object convertFromString(Map map, String[] strings, Class aClass) {
        School school;
        String[] schoolStrings = strings[0].split(",");
        for (String string : schoolStrings) {
            school = new School();
            school.setSchoolType(string.split(":")[0]);
            school.setSchoolName(string.split(":")[1]);
            schoolList.add(school);
        }
        return schoolList;
    }

    @Override
    public String convertToString(Map map, Object o) {
        schoolList = (List<School>) o;
        StringBuilder schoolString = new StringBuilder();
        for (School school : schoolList) {
            schoolString.append(school.getSchoolType())
                    .append(":")
                    .append(school.getSchoolName());
        }
        return schoolString.toString();
    }
}
